{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7bef0e88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultiModalConversationResponse(status_code=<HTTPStatus.OK: 200>, request_id='48bffed2-7df7-9ac0-ac65-53508d1592cb', code='', message='', output=MultiModalConversationOutput(choices=[Choice(finish_reason='stop', message=Message({'role': 'assistant', 'content': [{'text': '好的，以下是整理后的生成 JSON 格式内容：\\n\\n```json\\n{\\n    \"客户名称\": \"\",\\n    \"联繫方式\": \"\",\\n    \"产品型号\": \"\",\\n    \"生产日期\": \"\",\\n    \"数量\": 0,\\n    \"使用年限\": null,\\n    \"严重程度\": \"\",\\n    \"急紧程度\": \"\",\\n    \"问题点\": [],\\n    \"退货\": false,\\n    \"换货\": false,\\n    \"维修\": false,\\n    \"图例说明\": \"\",\\n    \"描述人/提报人\": {\\n        \"__DATE__\": \"\"\\n    },\\n    \"分析人\": {\\n        \"__DATE__\": \"\"\\n    },\\n    \"原因归属\": [\\n        \"\"\\n    ],\\n    \"零时措施\": {},\\n    \"改善措施\": {}\\n}\\n```\\n\\n请注意这个 JSON 对象中的键值对可能需要根据实际的表单结构进行调整。例如，“联系方 式”和“联系方式”的字段名应该是一致的；同样地，“严重程度”、“紧急程度”等也可能有误，请参照原图像自行修正。\\n\\n另外，在处理文本信息（如日期）的时候需要注意它们的具体形式，并在转换为 JSON 值之前正确解析这些数据。如果存在缺失或错误的数据项，则应相应地添加 `null` 或空字符串来表示该属性不存在或者没有提供具体的信息。'}]}))]), usage=MultiModalConversationUsage(input_tokens=1046, output_tokens=277))"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "import os\n",
    "import dashscope\n",
    "from dashscope.api_entities.dashscope_response import Role\n",
    "dashscope.api_key = \"sk-你的KEY\"\n",
    "from openai import OpenAI\n",
    "\n",
    "# 封装模型响应函数\n",
    "def get_response(messages):\n",
    "    response = dashscope.MultiModalConversation.call(\n",
    "        model='qwen-vl-plus',\n",
    "        messages=messages\n",
    "    )\n",
    "    return response\n",
    "\n",
    "content = [\n",
    "    {'image': 'https://aiwucai.oss-cn-huhehaote.aliyuncs.com/pdf_table.jpg'}, # Either a local path or an url\n",
    "    {'text': '这是一个表格图片，帮我提取里面的内容，输出JSON格式'}\n",
    "]\n",
    "\n",
    "messages=[{\"role\": \"user\", \"content\": content}]\n",
    "# 得到响应\n",
    "response = get_response(messages)\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e9ddbfd6-ddd7-4919-9f8e-94f0bd2f5472",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "好的，以下是整理后的生成 JSON 格式内容：\n",
      "\n",
      "```json\n",
      "{\n",
      "    \"客户名称\": \"\",\n",
      "    \"联繫方式\": \"\",\n",
      "    \"产品型号\": \"\",\n",
      "    \"生产日期\": \"\",\n",
      "    \"数量\": 0,\n",
      "    \"使用年限\": null,\n",
      "    \"严重程度\": \"\",\n",
      "    \"急紧程度\": \"\",\n",
      "    \"问题点\": [],\n",
      "    \"退货\": false,\n",
      "    \"换货\": false,\n",
      "    \"维修\": false,\n",
      "    \"图例说明\": \"\",\n",
      "    \"描述人/提报人\": {\n",
      "        \"__DATE__\": \"\"\n",
      "    },\n",
      "    \"分析人\": {\n",
      "        \"__DATE__\": \"\"\n",
      "    },\n",
      "    \"原因归属\": [\n",
      "        \"\"\n",
      "    ],\n",
      "    \"零时措施\": {},\n",
      "    \"改善措施\": {}\n",
      "}\n",
      "```\n",
      "\n",
      "请注意这个 JSON 对象中的键值对可能需要根据实际的表单结构进行调整。例如，“联系方 式”和“联系方式”的字段名应该是一致的；同样地，“严重程度”、“紧急程度”等也可能有误，请参照原图像自行修正。\n",
      "\n",
      "另外，在处理文本信息（如日期）的时候需要注意它们的具体形式，并在转换为 JSON 值之前正确解析这些数据。如果存在缺失或错误的数据项，则应相应地添加 `null` 或空字符串来表示该属性不存在或者没有提供具体的信息。\n"
     ]
    }
   ],
   "source": [
    "print(response.output.choices[0].message.content[0]['text'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
